<?php
/**
 * User: 五哥派
 * Date: 2024/6/30
 * Tel(同WX): 17335592719
 */
namespace app\admin\service;

use app\admin\model\store\Order as OrderModel;
use app\admin\model\store\OrderItem as OrderItemModel;

use app\common\lib\wechat\WxOrder;



class Order extends BaseService{


    public function shipping($orderId){
        $mOrderModel = new OrderModel();
        $orderInfo = $mOrderModel->findDataByID($orderId);
        $transaction_id = $orderInfo['trade_id'];
        $out_trade_no = $orderInfo['order_no'];
        $mchid = config('wx.sub_mch_id');

        $tracking_no = $orderInfo['delivery_no'];
        $express_company = $orderInfo['delivery_code'];
        $upload_time = $orderInfo['delivery_time'];
        //$item_desc = $item_desc;//商品信息

        $mOrderItemModel = new OrderItemModel();
        $list = $mOrderItemModel->queryData(['order_id' => $orderId]);
        $item_desc = '';
        foreach ($list as $item){
            $item_desc .= $item['name'].'*'.$item['num'];
        }
        $openid = '';

        // 获取当前小程序信息
        $wxConfig = $this->getMpWxConfig();
        // 微信登录 (获取session_key)
        $wxOrder = new WxOrder($wxConfig['app_id'], $wxConfig['app_secret']);
        $result = $wxOrder->shipping($transaction_id, $mchid, $out_trade_no, $tracking_no, $express_company, $item_desc, $upload_time, $openid);
        dump($result);
        //!$result && throwError($WxUser->getError());
        //return $result;


    }



    /**
     * 获取微信小程序配置项
     * @return array
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function getMpWxConfig(): array
    {
        //$wxConfig = WxappSettingModel::getWxappConfig();
        //if (empty($wxConfig['app_id']) || empty($wxConfig['app_secret'])) {
        //    throwError('请到后台小程序设置填写AppID和AppSecret参数');
        //}
        $wxConfig = [
            'app_id' => config('wx.app_id'),
            'app_secret' => config('wx.app_secret'),
        ];

        return $wxConfig;
    }
}